#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>

// c array
int *twosum(int *array, int length, int target){

    int *idxes = (int *)malloc(2*sizeof(int));
    int temp_sum = 0;
    int i=0, j=length-1;
    while(temp_sum != target){
        temp_sum = array[i] + array[j];
        if (temp_sum<target) i++;
        if (temp_sum>target) j--;
    }
    idxes[0] = i;
    idxes[1] = j;
    return idxes;
}

// c++ class and vector
class Solution{
public:
    int *twosum(std::vector<int>, int);
};
int *Solution::twosum(std::vector<int> vecint, int target){
    int *idxes = (int *)malloc(2*sizeof(int));
    int length = vecint.size();
    int temp_sum = 0;
    int i=0, j=length-1;
    while(temp_sum != target){
        temp_sum = vecint[i] + vecint[j];
        if (temp_sum<target) i++;
        if (temp_sum>target) j--;
    }
    idxes[0] = i;
    idxes[1] = j;
    return idxes;
}

int main(void){

    // std::vector<int> v={2, 7, 11, 15}; // c++98 does't support this syntax
    int a[4] = {2, 7, 11, 15};
    std::vector<int> v(a, a+4); // initialized by array
    int tar = 9;
    int *result1 = twosum(a, 4, tar);
    printf("Target is {%d}. \n", tar);
    printf("Indexes by array: {%d} & {%d}, Corresponding to values {%d} and {%d}. \n", 
            result1[0], result1[1], a[result1[0]], a[result1[1]]);

    // class
    Solution solution;
    int *result2 = solution.twosum(v, tar);
    printf("Indexes by vector: {%d} & {%d}, Corresponding to values {%d} and {%d}. \n", 
            result2[0], result2[1], v[result2[0]], v[result2[1]]);

    free(result1);
    free(result2);
    return 0;
}